package utils

import (
	"bytes"
	"sort"
)

// UniqueByteArray 字节数组去重
func UniqueByteArray(elements [][]byte) [][]byte {
	sort.Slice(elements, func(i, j int) bool {
		return bytes.Compare(elements[i], elements[j]) == -1
	})

	res := make([][]byte, 1)
	res[0] = elements[0]
	for i := 1; i < len(elements); i++ {
		if bytes.Equal(elements[i], elements[i-1]) {
			continue
		} else {
			res = append(res, elements[i])
		}
	}
	return res
}

// StringArrayToByteArray 将 []string 转换为 [][]byte
func StringArrayToByteArray(elements []string) [][]byte {
	res := make([][]byte, len(elements))
	for i, str := range elements {
		res[i] = []byte(str)
	}
	return res
}

// ByteArrayToStringArray 将 [][]byte 转换为 []string
func ByteArrayToStringArray(elements [][]byte) []string {
	res := make([]string, len(elements))
	for i, bytes := range elements {
		res[i] = string(bytes)
	}
	return res
}
